/*********************************************************************************************************
Trigger Name: NMRDoNotCallLeadUpdate 
* Author: Accenture 
* Date: 02-March-2012  
* Requirement/Project Name: NMR 
* Requirement Description: When a Lead record is marked Do Not Call, the Corresponding Do Not Call record
                           gets marked accordingly.
                           
* Version Update: Added condition for 5 new Additional Phone Fields on Lead for NMR to update Do Not Call Records.                           
***********************************************************************************************************/

trigger NMRLeadDoNotCallCreateUpdate on Lead (before update) {
    
    List<Do_Not_Call__c> DNC = new List<Do_Not_Call__c>(); //List of Do Not Calls associated to the phone numbers of the leads.
    List<Do_Not_Call__c> dncUpdated = new List<Do_Not_Call__c>(); //Final Updated List
    List<Lead> leadDNC = new List<Lead>(); // List of Leads that triggered the code.
    Set<String> phoneSet = new Set<String>(); // Set of Phone numbers for the Leads that got triggered.
    Set<String> primaryPhoneSet = new Set<String>(); 
    Map<String, Lead> leadMap = new Map<String, Lead>(); 
    ID leadRecordTypeId = RecordTypeHelper.GetRecordTypeID('Lead','NMR Lead');
    ID leadRecordTypewithCustId = RecordTypeHelper.GetRecordTypeID('Lead','NMR Lead with CustomerId');
    List<String> dncPhonelist = new List<String>(); 
    ID dncRecordTypeId = RecordTypeHelper.GetRecordTypeID('Do_Not_Call__c','Do Not Call');
    Set<String>  dncPhoneSet = new Set<String>(); 
    List<Do_Not_Call__c> insertDNC = new List<Do_Not_Call__c>();
        
    // Collecting the Leads and the Phone numbers from the Leads for which Do Not Call need to be Updated.
    for(Lead DNCLead: Trigger.new){
        if( DNCLead.DoNotCall != Trigger.OldMap.get(DNCLead.Id).DoNotCall  && (DNCLead.RecordTypeId == leadRecordTypewithCustId || DNCLead.RecordTypeId == leadRecordTypeId) ){
         NMRUtil.processed.add( DNCLead.Id );            
         leadDNC.add( DNCLead );
         phoneSet.add( DNCLead.Phone );
         primaryPhoneSet.add( DNCLead.Phone );         
         leadMap.put( DNCLead.Phone, DNCLead );
      }
      
        if( DNCLead.Do_Not_Call_1__c != Trigger.OldMap.get(DNCLead.Id).Do_Not_Call_1__c  && (DNCLead.RecordTypeId == leadRecordTypewithCustId || DNCLead.RecordTypeId == leadRecordTypeId) ){
         NMRUtil.processed.add( DNCLead.Id );
         leadDNC.add( DNCLead );
         phoneSet.add( DNCLead.NMR_Additional_Phone_1__c );
         leadMap.put( DNCLead.NMR_Additional_Phone_1__c, DNCLead );
         System.debug('Phone Number 1');
      } 
      
        if( DNCLead.Do_Not_Call_2__c != Trigger.OldMap.get(DNCLead.Id).Do_Not_Call_2__c  && (DNCLead.RecordTypeId == leadRecordTypewithCustId || DNCLead.RecordTypeId == leadRecordTypeId) ){
         NMRUtil.processed.add( DNCLead.Id );
         leadDNC.add( DNCLead );
         phoneSet.add( DNCLead.NMR_Additional_Phone_2__c );
         leadMap.put( DNCLead.NMR_Additional_Phone_2__c, DNCLead );
         System.debug('Phone Number 2');
      } 
       
        if( DNCLead.Do_Not_Call_3__c != Trigger.OldMap.get( DNCLead.Id ).Do_Not_Call_3__c  && (DNCLead.RecordTypeId == leadRecordTypewithCustId || DNCLead.RecordTypeId == leadRecordTypeId) ){
         NMRUtil.processed.add( DNCLead.Id );
         leadDNC.add( DNCLead );
         phoneSet.add( DNCLead.NMR_Additional_Phone_3__c );
         leadMap.put( DNCLead.NMR_Additional_Phone_3__c, DNCLead );
      }
      
        if( DNCLead.Do_Not_Call_4__c != Trigger.OldMap.get(DNCLead.Id).Do_Not_Call_4__c  && (DNCLead.RecordTypeId == leadRecordTypewithCustId || DNCLead.RecordTypeId == leadRecordTypeId) ){
         NMRUtil.processed.add( DNCLead.Id );
         leadDNC.add( DNCLead );
         phoneSet.add( DNCLead.NMR_Additional_Phone_4__c );
         leadMap.put( DNCLead.NMR_Additional_Phone_4__c, DNCLead );
      }  
      
        if( DNCLead.Do_Not_Call_5__c != Trigger.OldMap.get(DNCLead.Id).Do_Not_Call_5__c  && (DNCLead.RecordTypeId == leadRecordTypewithCustId || DNCLead.RecordTypeId == leadRecordTypeId) ){
         NMRUtil.processed.add( DNCLead.Id );
         leadDNC.add( DNCLead );
         phoneSet.add( DNCLead.NMR_Additional_Phone_5__c );
         leadMap.put( DNCLead.NMR_Additional_Phone_5__c, DNCLead );
      }  
             
    }   
    
    NMRDoNotCall.nmrLeadDoNotCallCreateUpdate(leadMap,phoneSet,primaryPhoneSet,LeadDNC);
    
}